package com.leo.erp.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leo.erp.dao.UserBusinessMapper;
import com.leo.erp.dto.LogExecution;
import com.leo.erp.dto.UserBusinessExecution;
import com.leo.erp.entity.UserBusiness;
import com.leo.erp.enums.LogStateEnum;
import com.leo.erp.enums.UserBusinessStateEnum;
import com.leo.erp.service.LogService;
import com.leo.erp.service.UserBusinessService;
import com.leo.erp.util.GlobalRuntimeException;

@Service
public class UserBusinessServiceImpl implements UserBusinessService{
	@Autowired
	private UserBusinessMapper userBusinessMapper;
	@Autowired
	private LogService logService;
	
	@Override
	public List<UserBusiness> getUserBusinessList(String type, String keyId, String deleteFlag) {	
		QueryWrapper<UserBusiness> queryWrapper = new QueryWrapper<>();
		
		if(type != null && type != "") {
			queryWrapper.eq("type", type);
		}
		if(keyId != null && keyId != "") {
			queryWrapper.eq("keyId", keyId);
		}
		if(deleteFlag != null && deleteFlag != "") {
			queryWrapper.eq("deleteFlag", deleteFlag);
		}	
		
		List<UserBusiness> userBusinessList = new ArrayList<>();
		try {
			userBusinessList = userBusinessMapper.selectList(queryWrapper);
		} catch (Exception e) {
			throw new GlobalRuntimeException(e.getMessage());
		}
		
		//返回结果集
		return userBusinessList;
	}

	@Override
	@Transactional
	public UserBusinessExecution insertUserBusiness(String beanJson, HttpServletRequest request)
			throws GlobalRuntimeException {
		UserBusiness userBusiness = JSONObject.parseObject(beanJson, UserBusiness.class);
		UserBusinessExecution ue = new UserBusinessExecution();
		int result = 0;
		try {
			result = userBusinessMapper.insert(userBusiness);
			LogExecution le = logService.insertLog("关联关系", "新增", ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
			if(result > 0 && le.getState() == LogStateEnum.SUCCESS.getState()) {
				ue.setState(UserBusinessStateEnum.SUCCESS.getState());//1
			}else {
				ue.setState(UserBusinessStateEnum.OFFLINE.getState());//-1
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return ue;
	}

	@Override
	public UserBusinessExecution checkIsUserBusinessExist(String TypeVale, String KeyIdValue, String UBValue) {
		UserBusinessExecution ue = new UserBusinessExecution();
		QueryWrapper<UserBusiness> queryWrapper = new QueryWrapper<>();
		String newValue = "%" + UBValue + "%";
		if(TypeVale != null && KeyIdValue != null) {
			queryWrapper.eq("type", TypeVale);
			queryWrapper.eq("keyId", KeyIdValue);
			queryWrapper.like("value", newValue);
			queryWrapper.ne("deleteFlag", "1");
		}else {
			queryWrapper.like("value", newValue);
			queryWrapper.ne("deleteFlag", "1");
		}
		try {
			List<UserBusiness> list = userBusinessMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				ue.setState(UserBusinessStateEnum.SUCCESS.getState());
			}else {
				ue.setState(UserBusinessStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return ue;
	}

	@Override
	public UserBusinessExecution updateUserBusiness(UserBusiness userBusiness) {
		UserBusinessExecution ue = new UserBusinessExecution();
		try {
			int effectedNum = userBusinessMapper.updateById(userBusiness);
			if(effectedNum > 0) {
				logService.insertLog("关联关系", "修改", ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				ue.setState(UserBusinessStateEnum.SUCCESS.getState());
			}else {
				ue.setState(UserBusinessStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return ue;
	}

	@Override
	public UserBusinessExecution checkIsValueExist(String type, String keyId) {
		UserBusinessExecution ue = new UserBusinessExecution();
		QueryWrapper<UserBusiness> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("type", type);
		queryWrapper.eq("keyId", keyId);
		queryWrapper.ne("deleteFlag", "1");
		
		try {
			List<UserBusiness> list = userBusinessMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				//存在,返回信息,获取id
				ue.setState(UserBusinessStateEnum.SUCCESS.getState());
				ue.setUserBusiness(list.get(0));
			}else {
				ue.setState(UserBusinessStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		
		return ue;
	}

	@Override
	public UserBusinessExecution getBasicData(String keyId, String type) {
		UserBusinessExecution ue = new UserBusinessExecution();
		QueryWrapper<UserBusiness> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("keyId", keyId);
		queryWrapper.eq("type", type);
		queryWrapper.ne("deleteFlag", "1");
		try {
			List<UserBusiness> list = userBusinessMapper.selectList(queryWrapper);
			if(list.size() > 0) {
				ue.setState(UserBusinessStateEnum.SUCCESS.getState());
				ue.setUserBusinessList(list);
			}else {
				ue.setState(UserBusinessStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return ue;
	}

	@Override
	public UserBusinessExecution updateBtnStr(int userBusinessId, String btnStr) {
		UserBusinessExecution ue = new UserBusinessExecution();
		//先获取该id的信息
		UserBusiness userBusiness = userBusinessMapper.selectById(userBusinessId);
		userBusiness.setBtnStr(btnStr);
		try {
			int effectedNum = userBusinessMapper.updateById(userBusiness);
			if(effectedNum > 0 ) {
				logService.insertLog("关联关系", "修改id=" + userBusinessId, ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				ue.setState(UserBusinessStateEnum.SUCCESS.getState());
			}else {
				ue.setState(UserBusinessStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return ue;
	}

}
